package com.taojiazheng.serviceImpl.job;

import com.taojiazheng.common.ApplicationException;
import com.taojiazheng.dao.job.JobDao;
import com.taojiazheng.dao.job.JobEvaluatesDao;
import com.taojiazheng.dao.order.OrderDao;
import com.taojiazheng.dao.user.UserDao;
import com.taojiazheng.entity.*;
import com.taojiazheng.mapper.UserinfoMapper;
import com.taojiazheng.service.job.JobService;
import com.taojiazheng.so.job.JobEvaluatesSo;
import com.taojiazheng.so.job.JobSo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赵成林
 * @date 2017/11/23 14:40
 */
@Service("jobService")
public class JobServiceImpl implements JobService {
    @Autowired
    private JobDao jobDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private JobEvaluatesDao jobEvaluatesDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private UserinfoMapper userinfoMapper;
    /**
     * 创建新的工作
     * 设计的表
     * job和jobinfo
     * 同时添加到我的发布中
     * @param jobSo
     * @param token
     * @return
     */
    public int createJob(JobSo jobSo, String token) {
        Job job = new Job();
        job.setJobType(jobSo.getJobType());
        User user = userDao.getUserByToken(token);
        int userId = user.getId();
        if(!isUserInfo(userId)){
            throw new ApplicationException(222,"请先实名认证，再来发布需求");
        }
        job.setUserId(userId);
        int jobId = jobDao.createJob(job);
        Jobinfo jobinfo = new Jobinfo();
        jobinfo.setInfo(jobSo.getInfo());
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String createDate = simpleDateFormat.format(date);
        jobinfo.setDate(createDate);
        jobinfo.setJobId(jobId);
        jobinfo.setIsHot(jobSo.getIsHot());
        jobinfo.setJobType(jobSo.getJobType());
        jobinfo.setNeedNum(jobSo.getNeedNum());
        jobinfo.setPrice(jobSo.getPrice());
        jobinfo.setSalary(jobSo.getSalary());
        jobinfo.setVisitNum(0);
        jobinfo.setTitle(jobSo.getTitle());
        jobDao.createJobInfo(jobinfo);
        // 封装mycreate参数
        Mycreate mycreate = new Mycreate();
        mycreate.setJobId(jobId);
        mycreate.setUserId(userId);
        mycreate.setDate(createDate);
        this.myCreate(mycreate);
        return jobId;
    }

    /**
     * 判断是否已经完善个人信息
     * @param userId
     * @return
     */
    protected boolean isUserInfo(Integer userId){

        Userinfo userinfo = userinfoMapper.getUserByUserId(userId);
        if(null == userinfo){
            return false;
        }
        return true;
    }

    /**
     * 把发布的信息添加到
     * 我的发布中
     */
    protected int myCreate(Mycreate mycreate){
        return jobDao.myCreate(mycreate);
    }

    public int createJobEvaluate(JobEvaluatesSo so, String token) {
        Jobevaluate jobevaluate = new Jobevaluate();
        User user = userDao.getUserByToken(token);
        boolean value = this.isCreateEvaluate(user.getId(),so.getJobId());
        if(!value){
            throw new ApplicationException(210,"请确认购买该工作并点击完成再来评价！");
        }
        jobevaluate.setDate(new Date());
        jobevaluate.setEvaluates(so.getEvaluates());
        jobevaluate.setJobId(so.getJobId());
        jobevaluate.setScore(so.getScore());
        jobevaluate.setUserId(user.getId());
        return jobEvaluatesDao.createJobEvaluate(jobevaluate);
    }

    /**
     * 判断用户是否可以对岗位评价
     * @param userId
     * @param jobId
     * @return
     */
    protected boolean isCreateEvaluate(Integer userId,Integer jobId){
        Map<String,Integer> hashmap = new HashMap<String, Integer>();
        hashmap.put("userId",userId);
        hashmap.put("jobId",jobId);
        // 因为一个用户对一个岗位可以购买多次，所以会得到一个list
        List<Order> orders = orderDao.getOrderByUserId(hashmap);
        if(orders == null){
            return false;
        }
        for(Order order : orders){
            if(order.getStatus() != 2){
                return false;
            }
        }
        return true;
    }
}
